package com.hctforgreen.greenservice.ctr;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;

import com.hctforgreen.greenservice.db.DbEntryAdapter;
import com.hctforgreen.greenservice.model.BookListEntity.BookEntity;
import com.hctforgreen.greenservice.model.BookZipTxtEntity;
import com.hctforgreen.greenservice.model.BookZipTxtEntity.EntryEntity;
import com.hctforgreen.greenservice.model.BookZipTxtEntity.EntryTitleEntity;
import com.hctforgreen.greenservice.model.EntryCatalogEntity;
import com.hctforgreen.greenservice.model.EntryCatalogEntity.EntryLevel1Entity;
import com.hctforgreen.greenservice.model.EntryCatalogEntity.EntryLevel1Entity.EntryLevel2Entity;
import com.hctforgreen.greenservice.model.EntryCatalogEntity.EntryLevel1Entity.EntryLevel2Entity.EntryLevel3Entity;
import com.hctforgreen.greenservice.model.EntryCatalogEntity.EntryLevel1Entity.EntryLevel2Entity.EntryLevel3Entity.EntryLevel4Entity;
import com.hctforgreen.greenservice.model.SerieListEntity.ChildSerieEntity;
import com.hctforgreen.greenservice.service.HctApplication;
import com.hctforgreen.greenservice.utils.HctConstants;

/**
 * db的控制器
 * 
 * @author Tallen Zhai
 * 
 */
public class DbEntryController {

	private DbEntryAdapter mDbAdapter;

	public DbEntryController(Context context) {
		mDbAdapter = ((HctApplication) context.getApplicationContext())
				.getDbEntryAdapter();
	}

	public DbEntryController(DbEntryAdapter dbAdapter) {
		mDbAdapter = dbAdapter;
	}

	// ============================
	// delete
	// ============================

	// ============================
	// Entity insertOrUpdate
	// ============================

	// ============================
	// Entity update
	// ============================

	// ============================
	// ListEntity insert
	// ============================
	/**
	 * 插入BookZipTxtEntity
	 * 
	 * @param bookZipTxtEntity
	 * @return
	 */
	public boolean insert(BookZipTxtEntity bookZipTxtEntity, String bookId) {
		boolean bState = false;
		mDbAdapter.beginTransaction();

		try {
			for (EntryTitleEntity entity : bookZipTxtEntity.entryTitleList) {
				if (insert(entity)) {
					bState = true;
				}
			}
			for (EntryEntity entity : bookZipTxtEntity.entryList) {
				if (insert(entity, bookId)) {
					bState = true;
				}
			}
			mDbAdapter.setTransactionSuccessful();
		} finally {
			mDbAdapter.endTransaction();
		}
		return bState;
	}

	// ============================
	// Entity insert
	// ============================
	public boolean insert(EntryTitleEntity entity) {
		if (isExists(entity)) {
			return false;
		} else {
			String sql = "insert into "
					+ HctConstants.DB_TABLE_NAME_ENTRY_TITLE_LST + " (id,"
					+ "parentId," + "level," + "bookId," + "titleContents)"
					+ " values " + "(?,?,?,?,?);";
			mDbAdapter.execSQL(sql, new Object[] { entity.id, entity.parentId,
					entity.level, entity.bookId, entity.titleContents });
			return true;
		}
	}

	public boolean insert(EntryEntity entity, String bookId) {
		if (isExists(entity)) {
			return false;
		} else {
			String sql = "insert into " + HctConstants.DB_TABLE_NAME_ENTRY_LST
					+ " (entryTitleId," + "bookId," + "contents)" + " values "
					+ "(?,?,?);";
			mDbAdapter.execSQL(sql, new Object[] { entity.entryTitleId, bookId,
					entity.contents });
			return true;
		}
	}

	// ============================
	// Entity isExists
	// ============================
	/**
	 * 判断此条EntryEntity在db中是否已经存在
	 * 
	 * @param entity
	 * @return
	 */
	public boolean isExists(EntryEntity entity) {
		String sql = "select count(*) from "
				+ HctConstants.DB_TABLE_NAME_ENTRY_LST
				+ " where entryTitleId='" + entity.entryTitleId + "'";
		Cursor cursor = mDbAdapter.rawQuery(sql, null);
		cursor.moveToFirst();
		if (cursor.getInt(0) > 0) {
			cursor.close();
			return true;
		} else {
			cursor.close();
			return false;
		}
	}

	/**
	 * 判断此条EntryTitleEntity在db中是否已经存在
	 * 
	 * @param entity
	 * @return
	 */
	public boolean isExists(EntryTitleEntity entity) {
		String sql = "select count(*) from "
				+ HctConstants.DB_TABLE_NAME_ENTRY_TITLE_LST + " where id='"
				+ entity.id + "'";
		Cursor cursor = mDbAdapter.rawQuery(sql, null);
		cursor.moveToFirst();
		if (cursor.getInt(0) > 0) {
			cursor.close();
			return true;
		} else {
			cursor.close();
			return false;
		}
	}

	// ============================
	// Entity Lst
	// ============================

	// ============================
	// Entity Lst
	// ============================
	/**
	 * 单本书的目录(模糊搜索)
	 * 
	 * @param boodId
	 * @param sSearchKey
	 * @return
	 */
	private EntryCatalogEntity fuzzySearchEntryCatalogEntityLst(
			final String bookId, final String sSearchKey) {
		EntryCatalogEntity bean = new EntryCatalogEntity();
		Cursor cursor = searchEntryTitleLevel1EntityLstCursor(bookId,
				sSearchKey);

		// mActivity.startManagingCursor(cursor);

		for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
			EntryLevel1Entity entity = new EntryLevel1Entity();
			int _idIndex = cursor.getColumnIndex("_id");
			int idIndex = cursor.getColumnIndex("id");
			int bookIdIndex = cursor.getColumnIndex("bookId");
			int parentIdIndex = cursor.getColumnIndex("parentId");
			int levelIndex = cursor.getColumnIndex("level");
			int titleContentsIndex = cursor.getColumnIndex("titleContents");

			entity._id = cursor.getInt(_idIndex);
			entity.id = cursor.getString(idIndex);
			entity.bookId = cursor.getString(bookIdIndex);
			entity.parentId = cursor.getString(parentIdIndex);
			entity.level = cursor.getString(levelIndex);
			entity.titleContents = cursor.getString(titleContentsIndex);

			if (bean.dataList == null) {
				bean.dataList = new ArrayList<EntryLevel1Entity>();
			}
			bean.dataList.add(searchAddEntryLevel2Lst(entity, sSearchKey));
		}
		// mActivity.stopManagingCursor(cursor);
		cursor.close();

		return bean;
	}

	/**
	 * 单本书的目录(模糊搜索)
	 * 
	 * @param boodId
	 * @param sSearchKey
	 * @return
	 */
	public EntryCatalogEntity fuzzySearchEntryCatalogEntity(
			final String bookId, final String sSearchKey) {
		EntryCatalogEntity bean = fuzzySearchEntryCatalogEntityLst(bookId,
				sSearchKey);

		return bean;
	}

	/**
	 * 多本书的目录(模糊搜索)
	 * 
	 * @param childSerieEntity
	 * @param sSearchKey
	 * @return
	 */
	private EntryCatalogEntity searchEntryMultiLevel1Lst(
			final ChildSerieEntity childSerieEntity, final String sSearchKey) {
		EntryCatalogEntity bean = new EntryCatalogEntity();
		for (BookEntity bookEntity : childSerieEntity.dataList) {
			Cursor cursor = searchEntryTitleLevel1EntityLstCursor(
					bookEntity.id, sSearchKey);

			// mActivity.startManagingCursor(cursor);

			for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor
					.moveToNext()) {
				EntryLevel1Entity entity = new EntryLevel1Entity();
				int _idIndex = cursor.getColumnIndex("_id");
				int idIndex = cursor.getColumnIndex("id");
				int bookIdIndex = cursor.getColumnIndex("bookId");
				int parentIdIndex = cursor.getColumnIndex("parentId");
				int levelIndex = cursor.getColumnIndex("level");
				int titleContentsIndex = cursor.getColumnIndex("titleContents");

				entity._id = cursor.getInt(_idIndex);
				entity.id = cursor.getString(idIndex);
				entity.bookId = cursor.getString(bookIdIndex);
				entity.parentId = cursor.getString(parentIdIndex);
				entity.level = cursor.getString(levelIndex);
				entity.titleContents = cursor.getString(titleContentsIndex);

				if (bean.dataList == null) {
					bean.dataList = new ArrayList<EntryLevel1Entity>();
				}
				bean.dataList.add(searchAddEntryLevel2Lst(entity, sSearchKey));
			}
			// mActivity.stopManagingCursor(cursor);
			cursor.close();
		}

		return bean;
	}

	/**
	 * 模糊搜索出二级
	 * 
	 * @param bean
	 * @return
	 */
	private EntryLevel1Entity searchAddEntryLevel2Lst(EntryLevel1Entity bean,
			String searchKey) {
		Cursor cursor = searchEntryTitleLevel2EntityLstCursor(bean.bookId,
				searchKey, bean.id);

		// mActivity.startManagingCursor(cursor);

		for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
			EntryLevel2Entity entity = new EntryLevel2Entity();
			int _idIndex = cursor.getColumnIndex("_id");
			int idIndex = cursor.getColumnIndex("id");
			int bookIdIndex = cursor.getColumnIndex("bookId");
			int parentIdIndex = cursor.getColumnIndex("parentId");
			int levelIndex = cursor.getColumnIndex("level");
			int titleContentsIndex = cursor.getColumnIndex("titleContents");

			entity._id = cursor.getInt(_idIndex);
			entity.id = cursor.getString(idIndex);
			entity.bookId = cursor.getString(bookIdIndex);
			entity.parentId = cursor.getString(parentIdIndex);
			entity.level = cursor.getString(levelIndex);
			entity.titleContents = cursor.getString(titleContentsIndex);

			if (bean.dataList == null) {
				bean.dataList = new ArrayList<EntryLevel2Entity>();
			}
			bean.dataList.add(searchAddEntryLevel3Lst(entity, searchKey));
		}
		// mActivity.stopManagingCursor(cursor);
		cursor.close();
		return bean;
	}

	/**
	 * 模糊搜索三级
	 * 
	 * @param bean
	 * @return
	 */
	private EntryLevel2Entity searchAddEntryLevel3Lst(EntryLevel2Entity bean,
			String searchKey) {
		Cursor cursor = searchEntryTitleLevel2EntityLstCursor(bean.bookId,
				searchKey, bean.id);

		// mActivity.startManagingCursor(cursor);

		for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
			EntryLevel3Entity entity = new EntryLevel3Entity();
			int _idIndex = cursor.getColumnIndex("_id");
			int idIndex = cursor.getColumnIndex("id");
			int bookIdIndex = cursor.getColumnIndex("bookId");
			int parentIdIndex = cursor.getColumnIndex("parentId");
			int levelIndex = cursor.getColumnIndex("level");
			int titleContentsIndex = cursor.getColumnIndex("titleContents");

			entity._id = cursor.getInt(_idIndex);
			entity.id = cursor.getString(idIndex);
			entity.bookId = cursor.getString(bookIdIndex);
			entity.parentId = cursor.getString(parentIdIndex);
			entity.level = cursor.getString(levelIndex);
			entity.titleContents = cursor.getString(titleContentsIndex);

			if (bean.dataList == null) {
				bean.dataList = new ArrayList<EntryLevel3Entity>();
			}
			bean.dataList.add(searchAddEntryLevel4Lst(entity, searchKey));
		}
		// mActivity.stopManagingCursor(cursor);
		cursor.close();
		return bean;
	}

	private EntryLevel3Entity searchAddEntryLevel4Lst(EntryLevel3Entity bean,
			String searchKey) {
		Cursor cursor = searchEntryTitleLevel2EntityLstCursor(bean.bookId,
				searchKey, bean.id);

		// mActivity.startManagingCursor(cursor);

		for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
			EntryLevel4Entity entity = new EntryLevel4Entity();
			int _idIndex = cursor.getColumnIndex("_id");
			int idIndex = cursor.getColumnIndex("id");
			int bookIdIndex = cursor.getColumnIndex("bookId");
			int parentIdIndex = cursor.getColumnIndex("parentId");
			int levelIndex = cursor.getColumnIndex("level");
			int titleContentsIndex = cursor.getColumnIndex("titleContents");

			entity._id = cursor.getInt(_idIndex);
			entity.id = cursor.getString(idIndex);
			entity.bookId = cursor.getString(bookIdIndex);
			entity.parentId = cursor.getString(parentIdIndex);
			entity.level = cursor.getString(levelIndex);
			entity.titleContents = cursor.getString(titleContentsIndex);

			if (bean.dataList == null) {
				bean.dataList = new ArrayList<EntryLevel4Entity>();
			}
			bean.dataList.add(entity);
		}
		// mActivity.stopManagingCursor(cursor);
		cursor.close();
		return bean;
	}

	/**
	 * 多本书的目录(模糊搜索)
	 * 
	 * @param childSerieEntity
	 * @param sSearchKey
	 * @return
	 */
	public EntryCatalogEntity getEntryMultiLevelCatalogEntity(
			final ChildSerieEntity childSerieEntity, final String sSearchKey) {
		EntryCatalogEntity bean = searchEntryMultiLevel1Lst(childSerieEntity,
				sSearchKey);

		return bean;
	}

	public String getEntryEntityContents(String bookId, String entryTitleId) {
		Cursor cursor = getEntryEntityCursor(bookId, entryTitleId);
		/**
		 * 把获取的Cursor对象交给Activity管理， 这样Cursor的生命周期便能和Activity自动同步， 省去自己手动管理。
		 */
		// mActivity.startManagingCursor(cursor);
		String contents = "";
		cursor.moveToFirst();
		if (!cursor.isAfterLast()) {

			int contentsIndex = cursor.getColumnIndex("contents");

			contents = cursor.getString(contentsIndex);

		}

		// mActivity.stopManagingCursor(cursor);
		cursor.close();
		return contents;
	}

	public EntryEntity getEntryEntity(String bookId, String entryTitleId) {
		Cursor cursor = getEntryEntityCursor(bookId, entryTitleId);
		/**
		 * 把获取的Cursor对象交给Activity管理， 这样Cursor的生命周期便能和Activity自动同步， 省去自己手动管理。
		 */
		// mActivity.startManagingCursor(cursor);
		EntryEntity bean = new EntryEntity();
		cursor.moveToFirst();
		if (!cursor.isAfterLast()) {

			int _idIndex = cursor.getColumnIndex("_id");
			int entryTitleIdIndex = cursor.getColumnIndex("entryTitleId");
			int bookIdIndex = cursor.getColumnIndex("bookId");
			int contentsIndex = cursor.getColumnIndex("contents");

			bean._id = cursor.getInt(_idIndex);
			bean.entryTitleId = cursor.getString(entryTitleIdIndex);
			bean.bookId = cursor.getString(bookIdIndex);
			bean.contents = cursor.getString(contentsIndex)
					+ getMultiContentsUseParentTitleId(bookId,
							bean.entryTitleId);

		}

		// mActivity.stopManagingCursor(cursor);
		cursor.close();
		return bean;
	}

	public String getMultiContentsUseParentTitleId(String bookId,
			String parentTitleId) {
		Cursor cursor = getChildEntryTitleEntityLstCursor(bookId, parentTitleId);
		String contents = "";
		for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
			EntryEntity entity = new EntryEntity();

			int idIndex = cursor.getColumnIndex("id");
			int bookIdIndex = cursor.getColumnIndex("bookId");
			int titleContentsIndex = cursor.getColumnIndex("titleContents");

			entity.entryTitleId = cursor.getString(idIndex);
			entity.bookId = cursor.getString(bookIdIndex);
			entity.titleContents = cursor.getString(titleContentsIndex);

			String tmpContent = getEntryEntityContents(bookId,
					entity.entryTitleId);
			if (!tmpContent.equals("")) {
				contents += "<p align=left style=font-size:12pt;font-weight:bold;color:black>"
						+ entity.titleContents + "</p>" + tmpContent;
			} else if (tmpContent.equals("")) {
				String lastContent = lastGetMultiContentsUseParentTitleId(
						bookId, entity.entryTitleId);
				if (!lastContent.equals("")) {
					contents += "<p align=left style=font-size:12pt;font-weight:bold;color:black>"
							+ entity.titleContents + "</p>" + lastContent;
				}
			}

		}
		cursor.close();
		return contents;
	}

	public String lastGetMultiContentsUseParentTitleId(String bookId,
			String parentTitleId) {
		Cursor cursor = getChildEntryTitleEntityLstCursor(bookId, parentTitleId);
		String contents = "";
		for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
			EntryEntity entity = new EntryEntity();

			int idIndex = cursor.getColumnIndex("id");
			int bookIdIndex = cursor.getColumnIndex("bookId");
			int titleContentsIndex = cursor.getColumnIndex("titleContents");

			entity.entryTitleId = cursor.getString(idIndex);
			entity.bookId = cursor.getString(bookIdIndex);
			entity.titleContents = cursor.getString(titleContentsIndex);

			String tmpContent = getEntryEntityContents(bookId,
					entity.entryTitleId);
			if (!tmpContent.equals("")) {
				contents += "<p align=left style=font-size:12pt;font-weight:bold;color:black>"
						+ entity.titleContents + "</p>" + tmpContent;
			}

		}
		cursor.close();
		return contents;
	}

	public BookZipTxtEntity getMultiEntryEntityLst(String bookId,
			String entryTitleId) {
		/**
		 * 查的是DB_TABLE_NAME_ENTRY_TITLE_LST
		 */
		Cursor cursor = getChildEntryTitleEntityLstCursor(bookId, entryTitleId);
		// mActivity.startManagingCursor(cursor);

		BookZipTxtEntity bean = new BookZipTxtEntity();

		for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
			EntryEntity entity = new EntryEntity();

			int idIndex = cursor.getColumnIndex("id");
			int bookIdIndex = cursor.getColumnIndex("bookId");
			int titleContentsIndex = cursor.getColumnIndex("titleContents");

			entity.entryTitleId = cursor.getString(idIndex);
			entity.bookId = cursor.getString(bookIdIndex);
			entity.titleContents = cursor.getString(titleContentsIndex);

			entity.contents = getEntryEntityContents(bookId,
					entity.entryTitleId)
					+ getMultiContentsUseParentTitleId(bookId,
							entity.entryTitleId);

			if (bean.entryList == null) {
				bean.entryList = new ArrayList<EntryEntity>();
			}
			bean.entryList.add(entity);
		}
		// mActivity.stopManagingCursor(cursor);
		cursor.close();
		return bean;
	}

	/**
	 * 一本书的目录(精确搜索)
	 * 
	 * @param bookId
	 * @param titleContents
	 * @return
	 */
	public EntryCatalogEntity getEntryCatalogEntity(String bookId,
			String titleContents) {
		EntryCatalogEntity bean = getEntryLevel1Lst(bookId, titleContents);

		return bean;
	}

	private EntryCatalogEntity getEntryLevel1Lst(final String bookId,
			final String titleContents) {
		if (titleContents.equals("")) {
			Cursor cursor = getEntryLevel1EntityLstCursor(bookId);
			// mActivity.startManagingCursor(cursor);
			EntryCatalogEntity bean = new EntryCatalogEntity();
			for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor
					.moveToNext()) {
				EntryLevel1Entity entity = new EntryLevel1Entity();
				int _idIndex = cursor.getColumnIndex("_id");
				int idIndex = cursor.getColumnIndex("id");
				int bookIdIndex = cursor.getColumnIndex("bookId");
				int parentIdIndex = cursor.getColumnIndex("parentId");
				int levelIndex = cursor.getColumnIndex("level");
				int titleContentsIndex = cursor.getColumnIndex("titleContents");

				entity._id = cursor.getInt(_idIndex);
				entity.id = cursor.getString(idIndex);
				entity.bookId = cursor.getString(bookIdIndex);
				entity.parentId = cursor.getString(parentIdIndex);
				entity.level = cursor.getString(levelIndex);
				entity.titleContents = cursor.getString(titleContentsIndex);

				if (bean.dataList == null) {
					bean.dataList = new ArrayList<EntryLevel1Entity>();
				}
				bean.dataList.add(addEntryLevel2Lst(entity));
			}
			// mActivity.stopManagingCursor(cursor);
			cursor.close();
			return bean;
		} else if (!titleContents.equals("")) {
			EntryTitleEntity titleEntity = getEntryTitleEntityUseTitleContents(
					bookId, titleContents);
			if (titleEntity == null) {
				titleEntity = new EntryTitleEntity();
			}
			Cursor cursor = getEntryLevel2EntityLstCursor(bookId,
					titleEntity.id);
			// mActivity.startManagingCursor(cursor);
			EntryCatalogEntity bean = new EntryCatalogEntity();
			for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor
					.moveToNext()) {
				EntryLevel1Entity entity = new EntryLevel1Entity();
				int _idIndex = cursor.getColumnIndex("_id");
				int idIndex = cursor.getColumnIndex("id");
				int bookIdIndex = cursor.getColumnIndex("bookId");
				int parentIdIndex = cursor.getColumnIndex("parentId");
				int levelIndex = cursor.getColumnIndex("level");
				int titleContentsIndex = cursor.getColumnIndex("titleContents");

				entity._id = cursor.getInt(_idIndex);
				entity.id = cursor.getString(idIndex);
				entity.bookId = cursor.getString(bookIdIndex);
				entity.parentId = cursor.getString(parentIdIndex);
				entity.level = cursor.getString(levelIndex);
				entity.titleContents = cursor.getString(titleContentsIndex);

				if (bean.dataList == null) {
					bean.dataList = new ArrayList<EntryLevel1Entity>();
				}
				bean.dataList.add(addEntryLevel2Lst(entity));
			}
			// mActivity.stopManagingCursor(cursor);
			cursor.close();
			return bean;
		} else {
			return null;
		}

	}

	public EntryTitleEntity getEntryTitleEntityUseTitleContents(String bookId,
			String titleContents) {
		Cursor cursor = getEntryTitleEntityCursor(bookId, titleContents);
		// mActivity.startManagingCursor(cursor);

		EntryTitleEntity titleEntity = null;
		cursor.moveToFirst();

		if (!cursor.isAfterLast()) {
			titleEntity = new EntryTitleEntity();
			int _idIndex = cursor.getColumnIndex("_id");
			int idIndex = cursor.getColumnIndex("id");

			titleEntity._id = cursor.getInt(_idIndex);
			titleEntity.id = cursor.getString(idIndex);
		}
		// mActivity.stopManagingCursor(cursor);
		cursor.close();
		return titleEntity;
	}

	private EntryLevel1Entity addEntryLevel2Lst(EntryLevel1Entity bean) {
		Cursor cursor = getEntryLevel2EntityLstCursor(bean.bookId, bean.id);
		// mActivity.startManagingCursor(cursor);

		for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
			EntryLevel2Entity entity = new EntryLevel2Entity();
			int _idIndex = cursor.getColumnIndex("_id");
			int idIndex = cursor.getColumnIndex("id");
			int bookIdIndex = cursor.getColumnIndex("bookId");
			int parentIdIndex = cursor.getColumnIndex("parentId");
			int levelIndex = cursor.getColumnIndex("level");
			int titleContentsIndex = cursor.getColumnIndex("titleContents");

			entity._id = cursor.getInt(_idIndex);
			entity.id = cursor.getString(idIndex);
			entity.bookId = cursor.getString(bookIdIndex);
			entity.parentId = cursor.getString(parentIdIndex);
			entity.level = cursor.getString(levelIndex);
			entity.titleContents = cursor.getString(titleContentsIndex);

			if (bean.dataList == null) {
				bean.dataList = new ArrayList<EntryLevel2Entity>();
			}
			bean.dataList.add(addEntryLevel3Lst(entity));
		}
		// mActivity.stopManagingCursor(cursor);
		cursor.close();
		return bean;
	}

	private EntryLevel2Entity addEntryLevel3Lst(EntryLevel2Entity bean) {
		Cursor cursor = getEntryLevel3EntityLstCursor(bean.bookId, bean.id);
		// mActivity.startManagingCursor(cursor);

		for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
			EntryLevel3Entity entity = new EntryLevel3Entity();
			int _idIndex = cursor.getColumnIndex("_id");
			int idIndex = cursor.getColumnIndex("id");
			int bookIdIndex = cursor.getColumnIndex("bookId");
			int parentIdIndex = cursor.getColumnIndex("parentId");
			int levelIndex = cursor.getColumnIndex("level");
			int titleContentsIndex = cursor.getColumnIndex("titleContents");

			entity._id = cursor.getInt(_idIndex);
			entity.id = cursor.getString(idIndex);
			entity.bookId = cursor.getString(bookIdIndex);
			entity.parentId = cursor.getString(parentIdIndex);
			entity.level = cursor.getString(levelIndex);
			entity.titleContents = cursor.getString(titleContentsIndex);

			if (bean.dataList == null) {
				bean.dataList = new ArrayList<EntryLevel3Entity>();
			}
			bean.dataList.add(addEntryLevel4Lst(entity));
		}
		// mActivity.stopManagingCursor(cursor);
		cursor.close();
		return bean;
	}

	private EntryLevel3Entity addEntryLevel4Lst(EntryLevel3Entity bean) {
		Cursor cursor = getEntryLevel4EntityLstCursor(bean.bookId, bean.id);
		// mActivity.startManagingCursor(cursor);

		for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
			EntryLevel4Entity entity = new EntryLevel4Entity();
			int _idIndex = cursor.getColumnIndex("_id");
			int idIndex = cursor.getColumnIndex("id");
			int bookIdIndex = cursor.getColumnIndex("bookId");
			int parentIdIndex = cursor.getColumnIndex("parentId");
			int levelIndex = cursor.getColumnIndex("level");
			int titleContentsIndex = cursor.getColumnIndex("titleContents");

			entity._id = cursor.getInt(_idIndex);
			entity.id = cursor.getString(idIndex);
			entity.bookId = cursor.getString(bookIdIndex);
			entity.parentId = cursor.getString(parentIdIndex);
			entity.level = cursor.getString(levelIndex);
			entity.titleContents = cursor.getString(titleContentsIndex);

			if (bean.dataList == null) {
				bean.dataList = new ArrayList<EntryLevel4Entity>();
			}
			bean.dataList.add(entity);
		}
		// mActivity.stopManagingCursor(cursor);
		cursor.close();
		return bean;
	}

	// ============================
	// Cusrsor
	// ============================

	/**
	 * 模糊查询Book的一级目录,获取结果游标(Cursor), asc:最早insert的会先被查询到
	 * 
	 * @return
	 */
	private Cursor searchEntryTitleLevel1EntityLstCursor(String bookId,
			String searchKey) {
		String sql = "select * from "
				+ HctConstants.DB_TABLE_NAME_ENTRY_TITLE_LST
				+ " where bookId='" + bookId + "'"
				+ " and titleContents like'%" + searchKey + "%'"
				+ " order by _id asc";

		Cursor cursor = mDbAdapter.rawQuery(sql, null);
		return cursor;

	}

	/**
	 * 模糊查询Book的二级目录,获取结果游标(Cursor), asc:最早insert的会先被查询到
	 * 
	 * @param bookId
	 * @param parentId
	 * @return
	 */
	private Cursor searchEntryTitleLevel2EntityLstCursor(String bookId,
			String searchKey, String parentId) {
		String sql = "select * from "
				+ HctConstants.DB_TABLE_NAME_ENTRY_TITLE_LST
				+ " where bookId='" + bookId + "'" + " and parentId='"
				+ parentId + "'"
				// + " and titleContents like'%" + searchKey + "%'"
				+ " order by _id asc";

		Cursor cursor = mDbAdapter.rawQuery(sql, null);
		return cursor;

	}

	/**
	 * 查询指定Book的某parentId的子目录,没有使用level做限制 获取结果游标(Cursor), asc:最早insert的会先被查询到
	 * 
	 * @return
	 */
	private Cursor getChildEntryTitleEntityLstCursor(String bookId,
			String parentId) {
		String sql = "select * from "
				+ HctConstants.DB_TABLE_NAME_ENTRY_TITLE_LST
				+ " where bookId='" + bookId + "'" + " and parentId='"
				+ parentId + "'" + " order by _id asc";

		Cursor cursor = mDbAdapter.rawQuery(sql, null);
		return cursor;

	}

	/**
	 * 查询说明书内容列表,获取结果游标(Cursor), desc:最新insert的会先被查询到
	 * 
	 * @return
	 */
	private Cursor getEntryEntityCursor(String bookId, String entryTitleId) {
		String sql = "select * from " + HctConstants.DB_TABLE_NAME_ENTRY_LST
				+ " where entryTitleId='" + entryTitleId + "'"
				+ " and bookId='" + bookId + "'" + " order by _id desc";

		Cursor cursor = mDbAdapter.rawQuery(sql, null);
		return cursor;
	}

	private Cursor getEntryTitleEntityCursor(String bookId, String titleContents) {
		String sql = "select * from "
				+ HctConstants.DB_TABLE_NAME_ENTRY_TITLE_LST
				+ " where bookId='" + bookId + "'" + " and titleContents='"
				+ titleContents + "'" + " order by _id asc";

		Cursor cursor = mDbAdapter.rawQuery(sql, null);
		return cursor;
	}

	/**
	 * 查询指定Book的一级目录,获取结果游标(Cursor), asc:最早insert的会先被查询到
	 * 
	 * @return
	 */
	private Cursor getEntryLevel1EntityLstCursor(String bookId) {
		String sql = "select * from "
				+ HctConstants.DB_TABLE_NAME_ENTRY_TITLE_LST
				+ " where bookId='" + bookId + "'" + " and level='" + "1" + "'"
				+ " order by _id asc";

		Cursor cursor = mDbAdapter.rawQuery(sql, null);
		return cursor;

	}

	/**
	 * 查询指定Book的二级目录,获取结果游标(Cursor), asc:最早insert的会先被查询到
	 * 
	 * @return
	 */
	private Cursor getEntryLevel2EntityLstCursor(String bookId, String parentId) {
		String sql = "select * from "
				+ HctConstants.DB_TABLE_NAME_ENTRY_TITLE_LST
				+ " where bookId='" + bookId + "'" + " and parentId='"
				+ parentId + "'"
				// + " and level='" + "2" + "'"
				+ " order by _id asc";

		Cursor cursor = mDbAdapter.rawQuery(sql, null);
		return cursor;

	}

	/**
	 * 查询指定Book的三级目录,获取结果游标(Cursor), asc:最早insert的会先被查询到
	 * 
	 * @return
	 */
	private Cursor getEntryLevel3EntityLstCursor(String bookId, String parentId) {
		String sql = "select * from "
				+ HctConstants.DB_TABLE_NAME_ENTRY_TITLE_LST
				+ " where bookId='" + bookId + "'" + " and parentId='"
				+ parentId + "'"
				// + " and level='" + "3" + "'"
				+ " order by _id asc";

		Cursor cursor = mDbAdapter.rawQuery(sql, null);
		return cursor;

	}

	/**
	 * 查询指定Book的四级目录,获取结果游标(Cursor), asc:最早insert的会先被查询到
	 * 
	 * @return
	 */
	private Cursor getEntryLevel4EntityLstCursor(String bookId, String parentId) {
		String sql = "select * from "
				+ HctConstants.DB_TABLE_NAME_ENTRY_TITLE_LST
				+ " where bookId='" + bookId + "'" + " and parentId='"
				+ parentId + "'"
				// + " and level='" + "4" + "'"
				+ " order by _id asc";

		Cursor cursor = mDbAdapter.rawQuery(sql, null);
		return cursor;

	}
	// ============================
	// get _id
	// ============================

}
